import mysql.connector
from bs4 import BeautifulSoup

__file__ = 'mytools.py'
__author__ = 'Jerry Liu'
__date__ = '2016-04-22'


headers_base = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Encoding': 'gzip, deflate, sdch',
    'Accept-Language': 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,zh-TW;q=0.2',
    'Connection': 'keep-alive',
    'Host': 'www.zhihu.com',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36',
    'Referer': 'http://www.zhihu.com/',
}

def getBsByGet(_session, url):
    result = _session.get(
        url,
        headers=headers_base
    )
    return BeautifulSoup(result.text, "html.parser")

def getBsByPost(_session, url, data={}):
    result = _session.post(
        url,
        data=data,
        headers=headers_base
    )
    return BeautifulSoup(result.text, "html.parser")

def getJson(_session, url, data={}):
    result = _session.post(
        url,
        data=data,
        headers=headers_base
    )
    return result.json()

def saveMySql(data):
    conn = mysql.connector.connect(host='localhost', user='root', password='123456', database='python_crawl')
    cursor = conn.cursor()
    sql = 'insert into zhihu_follow(userid,fuserid,fusername,domain) values(%s,%s,%s,%s)'
    try:
        cursor.execute(sql, [data[0],data[1],data[2],data[3]])
    except BaseException as e:
        print(e)

    # 提交并关闭连接
    conn.commit()
    cursor.close()
    conn.close()
    print('完成')